import json
import os
if not os.environ.get('DJANGO_SETTINGS_MODULE'):
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'drf_demo.settings')


# 让Django进行一次初始化
import django
django.setup()


from rest_framework import serializers

# class User(object):
#     '''用户类'''
#     def __init__(self, name, age):
#         self.name = name
#         self.age = age

# class UserSerializer(serializers.Serializer):
#     '''用户序列化器类'''
    # name = serializers.CharField()
    # age = serializers.IntegerField(write_only=True)
    # age = serializers.IntegerField(read_only=True)
    # age = serializers.IntegerField(required=False)
    # age = serializers.IntegerField(default=20)
    # addr = serializers.CharField(default='默认地址')


# if __name__ == '__main__':
#     # 创建User对象
#     user = User(name='smart', age=18)
#
#     # 创建序列化器对象,传入被序列化的user对象
#     serializer = UserSerializer(user)
#
#     # 获取序列化之后的数据
#     res = serializer.data
#     print(res)

# if __name__ == '__main__':
#     # data = {'name':'admin', 'age': 30}
#     data = {'name':'laowang'}
#     serializer = UserSerializer(data=data)
#     res = serializer.is_valid()
#
#     if res:
#         print('校验通过:', serializer.validated_data)
#     else:
#         print('校验失败:', serializer.errors)


from booktest.models import BookInfo, HeroInfo
from booktest.serializers import BookInfoSerializer, HeroInfoSerializer


# if __name__ == '__main__':
#     # 查询获取图书对象
#     book = BookInfo.objects.get(id=2)
#     # 创建序列化器对象
#     serializer = BookInfoSerializer(book)
#
#     res = serializer.data
#
#     # 格式化输出
#     res = json.dumps(res, indent=1, ensure_ascii=False)
#     print(res)

# if __name__ == '__main__':
#     # 查询获取图书对象
#     book = BookInfo.objects.all()
#     # 创建序列化器对象
#     serializer = BookInfoSerializer(book, many=True)
#
#     res = serializer.data
#
#     # 格式化输出
#     res = json.dumps(res, indent=1, ensure_ascii=False)
#     print(res)

# if __name__ == '__main__':
#     hero = HeroInfo.objects.get(id=6)
#     serializer = HeroInfoSerializer(hero)
#     res = serializer.data
#     res = json.dumps(res, indent=1, ensure_ascii=False)
#     print(res)

# if __name__ == '__main__':
#     data = {'bittle': 'python', 'bpub_date': '2019-06-01'}
#     serializer = BookInfoSerializer(data=data)
#     res = serializer.is_valid()
#
#     if res:
#         print(serializer.validated_data)
#
#     else:
#         print(serializer.errors)

# if __name__ == '__main__':
#     data = {'btitle':'python', 'bpub_date':'2009-09-09'}
#     serializer = BookInfoSerializer(data=data)
#     res = serializer.is_valid()
#
#     if res:
#         print(serializer.validated_data)
#     else:
#         print(serializer.errors)

if __name__ == '__main__':
    # # 1.准备数据
    # req_data = {'btitle': 'python', 'bpub_date': '2019-06-01', 'bread': 21, 'bcomment': 20}
    # # 2.创建序列化器对象
    # serializer = BookInfoSerializer(data=req_data)
    #
    # # 3.数据校验
    # serializer.is_valid()
    # # 4.数据保存
    # serializer.save()
    # # 5.获取ｃｒｅａｔｅ方法返回的对象序列化之后的数据
    # res = serializer.data
    # print(res)
    book = BookInfo.objects.get(id=2)
    req_data = {'btitle': '西游记', 'bpub_date': '1989-06-01', 'bread': 20001, 'bcomment': 20000}
    # 创建序列化器对象
    serializer = BookInfoSerializer(book, data=req_data)
    # 数据校验
    serializer.is_valid()
    # 数据保存：此处save会调用序列化器类中的update方法
    serializer.save()
    # 获取update方法返回的对象序列化之后的数据
    res = serializer.data
    print(res)